package arithmetic2;

import designPatterns.stateDemo.StateMain;

/**
 * @author Ajie
 * @date 2019-08-24
 * @function
 * 给定一个字符串，请你找出其中不含有重复字符的 最长子串 的长度。
 *
 * 示例 1:
 *
 * 输入: "abcabcbb"
 * 输出: 3
 * 解释: 因为无重复字符的最长子串是 "abc"，所以其长度为 3。
 * 链接：https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
 */
public class T_003_LongestSubStringWithoutRepeat {
    public static void main(String[] args) {
//        System.out.println(lengthOfLongestSubstring("abcabcbb"));
        System.out.println(lengthOfLongestSubstring("dvdf"));
        System.out.println(lengthOfLongestSubstring("pwwkew"));
    }


    public static int lengthOfLongestSubstring(String s) {
        if (s==null || s.length()<1) return 0;
         int res = 0;
         int start = 0;
         int end =0;
         //方法1 查看每一个数的long ,子序列也需要查看

        //方法2 滑动窗口
        char[] chars = s.toCharArray();
        for (int i = 0; i < s.length(); i++) {

            boolean isRepeat = false;
            for (int j = start; j < i; j++) {
                if (chars[j] == chars[i]){
                    isRepeat = true;
                    start = j+1;
                    res = Math.max(res,i - start);
                    break;
                }
            }
            if (!isRepeat)
            res = Math.max(res,i - start +1);
        }

//        for (int i = 0; i < s.length(); i++) {
//            char aChar = chars[i];
//            boolean isRepeat = false;
//            for (int j = i+1; j < s.length(); j++) {
//                char ends = chars[j];
//                System.out.println(aChar+" "+ends +" "+(aChar == ends));
//                if (aChar == ends ){
//                    isRepeat = true;
//                    if (j-i>res){
//                        res = j-i;
//                        start = i;
//                        end = j;
//                    }
//                    break;
//                }
//            }
//            if (!isRepeat){
//                if (s.length()-i>res){
//                    System.out.println(i);
//                    res = s.length()-i;
//                }
//            }
//        }

        return res;
    }
}
